package com.sojson.project.service.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sojson.enums.EBalanceCount;
import com.sojson.enums.EBalanceType;
import com.sojson.project.dao.BalanceLogDao;
import com.sojson.project.entity.po.BalanceLog;
import com.sojson.project.entity.vo.BalanceLogVo;
import com.sojson.project.service.BalanceLogService;
import com.sojson.project.sys.user.dao.UserDao;

/**
 * 佣金记录表Service实现类
 * 
 * @author liu
 * @date 2021-03-16
 */
@Service
public class BalanceLogServiceImpl implements BalanceLogService {

    @Lazy
    @Autowired
    private BalanceLogServiceImpl balanceLogService;
    @Resource
    private BalanceLogDao balanceLogDaoImpl;
    @Resource
    private UserDao userDao;

    /**
     * 单条插入
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public int insert(BalanceLog balanceLog) {
        int insert = balanceLogDaoImpl.insert(balanceLog);
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public int insertList(BalanceLog balanceLog) {
        int insertList = balanceLogDaoImpl.insertList(balanceLog.getBalanceLogs());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByDto(BalanceLog balanceLog) {
        int deleteByDto = balanceLogDaoImpl.deleteByDto(balanceLog);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public int countAll() {
        int countAll = balanceLogDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public List<BalanceLog> findAll() {
        List<BalanceLog> findAll = balanceLogDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public int countByDto(BalanceLog balanceLog) {
        int countByDto = balanceLogDaoImpl.countByDto(balanceLog);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public List<BalanceLogVo> findByDto(BalanceLog balanceLog) {
        List<BalanceLogVo> findByDto = balanceLogDaoImpl.list(balanceLog);
        return findByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public List<BalanceLog> findXcxByDto(BalanceLog balanceLog) {
        List<BalanceLog> findByDto = balanceLogDaoImpl.findXcxByDto(balanceLog);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteById(Integer id) {
        int deleteById = balanceLogDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByIdList(ArrayList<Integer> ids) {
        int deleteByIdList = balanceLogDaoImpl.deleteByIdList(ids);
        return deleteByIdList;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public int updateById(BalanceLog balanceLog) {
        int updateById = balanceLogDaoImpl.updateById(balanceLog);
        return updateById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticByIdList(BalanceLog balanceLog) {
        int updateStaticByIdList = balanceLogDaoImpl.updateStaticByIdList(balanceLog);
        return updateStaticByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangeByIdList(BalanceLog balanceLog) {
        int updateChangeByIdList = balanceLogDaoImpl.updateChangeByIdList(balanceLog.getBalanceLogs());
        return updateChangeByIdList;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public int updatePartialById(BalanceLog balanceLog) {
        int updatePartialById = balanceLogDaoImpl.updatePartialById(balanceLog);
        return updatePartialById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticPartialByIdList(BalanceLog balanceLog) {
        int updateStaticPartialByIdList = balanceLogDaoImpl.updateStaticPartialByIdList(balanceLog);
        return updateStaticPartialByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangePartialByIdList(BalanceLog balanceLog) {
        int updateChangePartialByIdList = balanceLogDaoImpl.updateChangePartialByIdList(balanceLog.getBalanceLogs());
        return updateChangePartialByIdList;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public BalanceLog findById(Integer id) {
        BalanceLog findById = balanceLogDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<BalanceLog> findByIdList(ArrayList<Integer> ids) {
        ArrayList<BalanceLog> findByIdList = balanceLogDaoImpl.findByIdList(ids);
        return findByIdList;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<Integer> findIdByDto(BalanceLog balanceLog) {
        ArrayList<Integer> findIdByDto = balanceLogDaoImpl.findIdByDto(balanceLog);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     * 
     * @throws SQLException
     */
    @Override
    public void clear() {
        balanceLogDaoImpl.clear();
    }

    /**
     * 修改佣金金额
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public synchronized boolean updateBalance(BalanceLog balanceLog) {
        return balanceLogService.updateBalanceGo(balanceLog);
    }

    @Transactional(rollbackFor = Throwable.class)
    public boolean updateBalanceGo(BalanceLog balanceLog) {
        String id = balanceLog.getMid();
        // 获取佣金金额
        BigDecimal findBalanceById = userDao.findBalanceById(id);
        // 修改佣金金额
        userDao.updateBalanceById(id, findBalanceById.add(balanceLog.getTotal()));
        // 添加记录
        balanceLogDaoImpl.insert(balanceLog);
        return true;
    }

    /**
     * 根据对象查询推广列表
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public List<BalanceLogVo> findRegisterByDto(BalanceLogVo balanceLog) {
        balanceLog.setType(EBalanceType.REGISTER.getCode());
        return balanceLogDaoImpl.findRegisterByDto(balanceLog);
    }

    /**
     * 根据对象查询分销列表
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public List<BalanceLogVo> findDivideByDto(BalanceLogVo balanceLog) {
        balanceLog.setType(EBalanceType.DIVIDE.getCode());
        return balanceLogDaoImpl.findDivideByDto(balanceLog);
    }

    /**
     * 根据对象统计余额
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public BigDecimal sumBalance(BalanceLogVo balanceLog) {
        balanceLog.setCount(EBalanceCount.ADD.getCode());
        BigDecimal sumBalanceAdd = balanceLogDaoImpl.sumBalance(balanceLog);
        balanceLog.setCount(EBalanceCount.REDUCE.getCode());
        BigDecimal sumBalanceReduce = balanceLogDaoImpl.sumBalance(balanceLog);
        return sumBalanceAdd.subtract(sumBalanceReduce);
    }

    /**
     * 根据对象查询
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @Override
    public List<BalanceLogVo> findSjByDto(BalanceLog balanceLog) {
        return balanceLogDaoImpl.findSjByDto(balanceLog);
    }

}